#include "MSqlTest.h"
#include "MSql/MSqlServer.h"
#include "MSql/MSqlDatabase.h"

using namespace NsMSql;

MSqlTest::MSqlTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	//字符串测试
	{
		//QString str = CNString("电话号码");
		//qDebug() << str;
		//int d = 0;
	}
	//数据库测试
	{
		MSqlServerAttr sqlServerAttr;
		//服务器参数
		sqlServerAttr.type = eMySQL;
		sqlServerAttr.hostName = "localhost";
		sqlServerAttr.port = 3306;
		sqlServerAttr.userName = "root";
		sqlServerAttr.password = "123456";
		//数据库参数
		sqlServerAttr.databaseName = "test";
		//连接池参数
		sqlServerAttr.initialConnectNum = 1;
		sqlServerAttr.maxConnectionNum = 1000;

		//打开服务器
		MSqlServer sqlServer = MSqlServer::getInstance();
		bool open = sqlServer.open(sqlServerAttr);
		if (open)
		{
			QString databaseName = "test";
			QStringList databasenNameList = sqlServer.getDatabaseNameList();
			if (!databasenNameList.contains(databaseName))
				sqlServer.createDatabase("test");
			MSqlDatabase* pSqlDatabase = sqlServer.useDatabase("test");
			if (pSqlDatabase)	//创建表
			{
				QString tableName = "tb_test";
				QList<MTableField> tableFields;
				MTableField tableField;

				//ID
				tableField.fieldName = "ID";
				tableField.fieldDataType = NsMSql::eInt;
				tableField.primaryKey = true;
				tableField.primaryKeyIncrement = true;
				tableField.fieldDataLength = 3;
				tableField.remark = "ID";
				tableFields << tableField;
				//Name
				tableField.fieldName = "Name";
				tableField.fieldDataType = NsMSql::eVarchar;
				tableField.primaryKey = false;
				tableField.primaryKeyIncrement = false;
				tableField.fieldDataLength = 32;
				tableField.remark = CNString("名称");
				tableFields << tableField;
				//City
				tableField.fieldName = "City";
				tableField.fieldDataType = NsMSql::eVarchar;
				tableField.fieldDataLength = 32;
				tableField.remark = CNString("居住地");
				tableFields << tableField;
				//Tel
				tableField.fieldName = "Tel";
				tableField.fieldDataType = NsMSql::eVarchar;
				tableField.fieldDataLength = 32;
				tableField.remark = CNString("电话号码");
				tableFields << tableField;

				bool res = pSqlDatabase->createTable(tableName, tableFields);
				if (res)
				{
					
				}
			}
		}
	}
}
